#include <jni.h>
#include <string>
#include "Modalmass_estimation_newchangefinal1219.h"
#include "Frequency_dampingratio_estimation_and_plot1219.h"


extern "C"
JNIEXPORT jdoubleArray JNICALL
Java_com_tjvib_util_Manager_DataSetManager_fre_1res1(JNIEnv *env, jobject thiz, jdoubleArray a,
                                                      jdouble t0, jdouble t1, jdouble h,
                                                      jchar stru_material, jdoubleArray frequency,
                                                      jdoubleArray original_frequency, jdoubleArray dampingratio,
                                                     jdoubleArray original_dampingratio, jdoubleArray f,
                                                      jdoubleArray p) {

    double* _a = env->GetDoubleArrayElements(a, nullptr);
    double* _frequency = env->GetDoubleArrayElements(frequency, nullptr);
    double* _original_frequency = env->GetDoubleArrayElements(original_frequency, nullptr);
    double* _dampingratio = env->GetDoubleArrayElements(dampingratio, nullptr);
    double* _original_dampingratio = env->GetDoubleArrayElements(original_dampingratio, nullptr);
    double* _f = env->GetDoubleArrayElements(f, nullptr);
    double* _p = env->GetDoubleArrayElements(p, nullptr);

    Frequency_dampingratio_estimation_and_plot1219(_a, t0, t1, h, stru_material,
                                               _frequency, _original_frequency,
                                               _dampingratio, _original_dampingratio,
                                               _f, _p);

    jsize frequencyLength = env->GetArrayLength(frequency);
    jdoubleArray result = env->NewDoubleArray(frequencyLength);
    if (result == nullptr) {
        env->ReleaseDoubleArrayElements(a, _a, 0);
        env->ReleaseDoubleArrayElements(frequency, _frequency, 0);
        env->ReleaseDoubleArrayElements(original_frequency, _original_frequency, 0);
        env->ReleaseDoubleArrayElements(dampingratio, _dampingratio, 0);
        env->ReleaseDoubleArrayElements(original_dampingratio, _original_dampingratio, 0);
        env->ReleaseDoubleArrayElements(f, _f, 0);
        env->ReleaseDoubleArrayElements(p, _p, 0);
        return nullptr; // 返回空表示出错
    }

    env->SetDoubleArrayRegion(result, 0, frequencyLength, _frequency);
    env->SetDoubleArrayRegion(result, 0, frequencyLength, _original_frequency);

    env->ReleaseDoubleArrayElements(a, _a, 0);
    env->ReleaseDoubleArrayElements(frequency, _frequency, 0);
    env->ReleaseDoubleArrayElements(original_frequency, _original_frequency, 0);
    env->ReleaseDoubleArrayElements(dampingratio, _dampingratio, 0);
    env->ReleaseDoubleArrayElements(original_dampingratio, _original_dampingratio, 0);
    env->ReleaseDoubleArrayElements(f, _f, 0);
    env->ReleaseDoubleArrayElements(p, _p, 0);

    return result;
}


extern "C"
JNIEXPORT jdoubleArray JNICALL
Java_com_tjvib_util_Manager_DataSetManager_mass_1res1(JNIEnv *env, jobject thiz,
                                                       jdoubleArray person, jdoubleArray structure,
                                                       jdoubleArray f, jdouble weight, jdouble t0,
                                                       jdouble d, jdouble n, jdouble l, jdouble b,
                                                       jchar stru_system,
                                                       jdoubleArray original_Modalmass, jdoubleArray Modalmass,
                                                       jdoubleArray original_eachmass, jdoubleArray eachmass,
                                                       jdoubleArray original_k, jdoubleArray k) {

    double* _person = env->GetDoubleArrayElements(person, nullptr);
    double* _structure = env->GetDoubleArrayElements(structure, nullptr);
    double* _f = env->GetDoubleArrayElements(f, nullptr);
    double* _original_Modalmass = env->GetDoubleArrayElements(original_Modalmass, nullptr);
    double* _Modalmass = env->GetDoubleArrayElements(Modalmass, nullptr);
    double* _original_eachmass = env->GetDoubleArrayElements(original_eachmass, nullptr);
    double* _eachmass = env->GetDoubleArrayElements(eachmass, nullptr);
    double* _original_k = env->GetDoubleArrayElements(original_k, nullptr);
    double* _k = env->GetDoubleArrayElements(k, nullptr);

    Modalmass_estimation_newchangefinal1219(_person, _structure, _f, weight, t0,
                                            d, n, l, b, stru_system,
                                            _original_Modalmass, _Modalmass,
                                            _original_eachmass, _eachmass,
                                            _original_k, _k);

    jsize len = env->GetArrayLength(Modalmass);
    jdoubleArray result = env->NewDoubleArray(len);
    if (result == nullptr) {
        env->ReleaseDoubleArrayElements(person, _person, 0);
        env->ReleaseDoubleArrayElements(structure, _structure, 0);
        env->ReleaseDoubleArrayElements(f, _f, 0);
        env->ReleaseDoubleArrayElements(original_Modalmass, _original_Modalmass, 0);
        env->ReleaseDoubleArrayElements(Modalmass, _Modalmass, 0);
        env->ReleaseDoubleArrayElements(original_eachmass, _original_eachmass, 0);
        env->ReleaseDoubleArrayElements(eachmass, _eachmass, 0);
        env->ReleaseDoubleArrayElements(original_k, _original_k, 0);
        env->ReleaseDoubleArrayElements(k, _k, 0);
        return nullptr; // 返回空表示出错
    }

    env->SetDoubleArrayRegion(result, 0, len, _Modalmass);
    env->SetDoubleArrayRegion(result, 0, len, _original_Modalmass);

    env->ReleaseDoubleArrayElements(person, _person, 0);
    env->ReleaseDoubleArrayElements(structure, _structure, 0);
    env->ReleaseDoubleArrayElements(f, _f, 0);
    env->ReleaseDoubleArrayElements(original_Modalmass, _original_Modalmass, 0);
    env->ReleaseDoubleArrayElements(Modalmass, _Modalmass, 0);
    env->ReleaseDoubleArrayElements(original_eachmass, _original_eachmass, 0);
    env->ReleaseDoubleArrayElements(eachmass, _eachmass, 0);
    env->ReleaseDoubleArrayElements(original_k, _original_k, 0);
    env->ReleaseDoubleArrayElements(k, _k, 0);

    return result;
}

